/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package org.eclipse.iota.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EObjectContainmentWithInverseEList;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.iota.IotaPackage;
import org.eclipse.iota.util.IotaConstraintUtil;

/**
 * @see org.eclipse.iota.IotaPackage#getClause()
 * @model kind="class"
 * @generated
 */
public class Clause extends EObjectImpl implements Element, Predicate {

	/**
	 * @generated NOT
	 */
	public Clause(Constraint... constraints) {
		for (Constraint constraint : constraints) {
			getConstraints().add(constraint);
		}
	}

	/**
	 * @model kind="operation"
	 * @generated NOT
	 */
	public boolean isTrue() {
		// all constraints must be true
		for (Constraint constraint : getConstraints()) {
			if (!constraint.isTrue()) return false;
		}
		return true;
	}

	/**
	 * @model kind="operation"
	 * @generated NOT
	 */
	public boolean isFalse() {
		// at least one constraint must be false
		for (Constraint constraint : getConstraints()) {
			if (constraint.isFalse()) return true;
		}
		return false;		
	}

	/**
	 * @generated NOT
	 */
	@Override
	public String toString() {
		StringBuffer result = new StringBuffer();
		if (!getExistentials().isEmpty()) {
			result.append("exists ");
			for (int i=0; i<getExistentials().size(); i++) {
				result.append(getExistentials().get(i));
				if (i<getExistentials().size()-1) result.append(",");
			}
			result.append(" : ");
		}
		int lineLength = 0;
		for (int i=0; i<getConstraints().size(); i++) {
			String current = getConstraints().get(i).toString();
			lineLength += current.length();
			result.append(current);
			if (i<getConstraints().size()-1) {
				result.append(" & ");
				if (lineLength > 50) {
					result.append("\n\t\t\t");
					lineLength = 0;
				}
			}
		}
		return result.toString();
	}
	
	/**
	 * Sort the constraints in a clause. This is just for pretty-printing.
	 * @generated NOT
	 */
	public void sortConstraints() {
		List<Constraint> constraints = new ArrayList<Constraint>(getConstraints());
		Collections.sort(constraints, IotaConstraintUtil.COMPARATOR);
		getConstraints().clear();
		getConstraints().addAll(constraints);
	}
	
	/* ---------------------------------------------------------------- *
	 * GENERATED CODE.                                                  *
	 * Do not edit below this line. If you need to edit, move it above  *
	 * this line and change the '@generated'-tag to '@generated NOT'.   *
	 * ---------------------------------------------------------------- */	

	/**
	 * The cached value of the '{@link #getConstraints() <em>Constraints</em>}' containment reference list.
	 * @see #getConstraints()
	 * @generated
	 * @ordered
	 */
	protected EList<Constraint> constraints;
	
	/**
	 * The cached value of the '{@link #getExistentials() <em>Existentials</em>}' containment reference list.
	 * @see #getExistentials()
	 * @generated
	 * @ordered
	 */
	protected EList<Existential> existentials;

	/**
	 * @generated
	 */
	public Clause() {
		super();
	}

	/**
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return IotaPackage.Literals.CLAUSE;
	}

	/**
	 * Returns the value of the '<em><b>Constraints</b></em>' containment reference list.
	 * The list contents are of type {@link org.eclipse.iota.model.Constraint}.
	 * It is bidirectional and its opposite is '{@link org.eclipse.iota.model.Constraint#getClause <em>Clause</em>}'.
	 * @return the value of the '<em>Constraints</em>' containment reference list.
	 * @see org.eclipse.iota.IotaPackage#getClause_Constraints()
	 * @see org.eclipse.iota.model.Constraint#getClause
	 * @model opposite="clause" containment="true"
	 * @generated
	 */
	public EList<Constraint> getConstraints() {
		if (constraints == null) {
			constraints = new EObjectContainmentWithInverseEList<Constraint>(Constraint.class, this, IotaPackage.CLAUSE__CONSTRAINTS, IotaPackage.CONSTRAINT__CLAUSE);
		}
		return constraints;
	}

	/**
	 * Returns the value of the '<em><b>Existentials</b></em>' containment reference list.
	 * The list contents are of type {@link org.eclipse.iota.model.Existential}.
	 * @return the value of the '<em>Existentials</em>' containment reference list.
	 * @see org.eclipse.iota.IotaPackage#getClause_Existentials()
	 * @model containment="true"
	 * @generated
	 */
	public EList<Existential> getExistentials() {
		if (existentials == null) {
			existentials = new EObjectContainmentEList<Existential>(Existential.class, this, IotaPackage.CLAUSE__EXISTENTIALS);
		}
		return existentials;
	}

	/**
	 * Returns the value of the '<em><b>Alternation</b></em>' container reference.
	 * It is bidirectional and its opposite is '{@link org.eclipse.iota.model.Alternation#getClauses <em>Clauses</em>}'.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @return the value of the '<em>Alternation</em>' container reference.
	 * @see #setAlternation(Alternation)
	 * @see org.eclipse.iota.IotaPackage#getClause_Alternation()
	 * @see org.eclipse.iota.model.Alternation#getClauses
	 * @model opposite="clauses" transient="false"
	 * @generated
	 */
	public Alternation getAlternation() {
		if (eContainerFeatureID() != IotaPackage.CLAUSE__ALTERNATION) return null;
		return (Alternation)eContainer();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetAlternation(Alternation newAlternation, NotificationChain msgs) {
		msgs = eBasicSetContainer((InternalEObject)newAlternation, IotaPackage.CLAUSE__ALTERNATION, msgs);
		return msgs;
	}

	/**
	 * Sets the value of the '{@link org.eclipse.iota.model.Clause#getAlternation <em>Alternation</em>}' container reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @param value the new value of the '<em>Alternation</em>' container reference.
	 * @see #getAlternation()
	 * @generated
	 */
	public void setAlternation(Alternation newAlternation) {
		if (newAlternation != eInternalContainer() || (eContainerFeatureID() != IotaPackage.CLAUSE__ALTERNATION && newAlternation != null)) {
			if (EcoreUtil.isAncestor(this, newAlternation))
				throw new IllegalArgumentException("Recursive containment not allowed for " + toString());
			NotificationChain msgs = null;
			if (eInternalContainer() != null)
				msgs = eBasicRemoveFromContainer(msgs);
			if (newAlternation != null)
				msgs = ((InternalEObject)newAlternation).eInverseAdd(this, IotaPackage.ALTERNATION__CLAUSES, Alternation.class, msgs);
			msgs = basicSetAlternation(newAlternation, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, IotaPackage.CLAUSE__ALTERNATION, newAlternation, newAlternation));
	}

	/**
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case IotaPackage.CLAUSE__CONSTRAINTS:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getConstraints()).basicAdd(otherEnd, msgs);
			case IotaPackage.CLAUSE__ALTERNATION:
				if (eInternalContainer() != null)
					msgs = eBasicRemoveFromContainer(msgs);
				return basicSetAlternation((Alternation)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case IotaPackage.CLAUSE__CONSTRAINTS:
				return ((InternalEList<?>)getConstraints()).basicRemove(otherEnd, msgs);
			case IotaPackage.CLAUSE__EXISTENTIALS:
				return ((InternalEList<?>)getExistentials()).basicRemove(otherEnd, msgs);
			case IotaPackage.CLAUSE__ALTERNATION:
				return basicSetAlternation(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eBasicRemoveFromContainerFeature(NotificationChain msgs) {
		switch (eContainerFeatureID()) {
			case IotaPackage.CLAUSE__ALTERNATION:
				return eInternalContainer().eInverseRemove(this, IotaPackage.ALTERNATION__CLAUSES, Alternation.class, msgs);
		}
		return super.eBasicRemoveFromContainerFeature(msgs);
	}

	/**
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case IotaPackage.CLAUSE__CONSTRAINTS:
				return getConstraints();
			case IotaPackage.CLAUSE__EXISTENTIALS:
				return getExistentials();
			case IotaPackage.CLAUSE__ALTERNATION:
				return getAlternation();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case IotaPackage.CLAUSE__CONSTRAINTS:
				getConstraints().clear();
				getConstraints().addAll((Collection<? extends Constraint>)newValue);
				return;
			case IotaPackage.CLAUSE__EXISTENTIALS:
				getExistentials().clear();
				getExistentials().addAll((Collection<? extends Existential>)newValue);
				return;
			case IotaPackage.CLAUSE__ALTERNATION:
				setAlternation((Alternation)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case IotaPackage.CLAUSE__CONSTRAINTS:
				getConstraints().clear();
				return;
			case IotaPackage.CLAUSE__EXISTENTIALS:
				getExistentials().clear();
				return;
			case IotaPackage.CLAUSE__ALTERNATION:
				setAlternation((Alternation)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case IotaPackage.CLAUSE__CONSTRAINTS:
				return constraints != null && !constraints.isEmpty();
			case IotaPackage.CLAUSE__EXISTENTIALS:
				return existentials != null && !existentials.isEmpty();
			case IotaPackage.CLAUSE__ALTERNATION:
				return getAlternation() != null;
		}
		return super.eIsSet(featureID);
	}

}
